package com.douyu.water.core.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.douyu.water.core.PagePara;
import com.douyu.water.core.dao.AreaMapper;
import com.douyu.water.core.dao.RTU_WMapper;
import com.douyu.water.core.data.DataMapper;
import com.douyu.water.core.data.RtuMapper;
import com.douyu.water.core.model.Area;
import com.douyu.water.core.model.AreaExample;
import com.douyu.water.core.model.Data;
import com.douyu.water.core.model.DataExample;
import com.douyu.water.core.model.Result;
import com.douyu.water.core.model.Rtu;
import com.douyu.water.core.model.RtuExample;
import com.douyu.water.core.model.UserArea;
import com.douyu.water.core.model.WtldData;
import com.douyu.water.core.model.WtldRtu;
import com.douyu.water.core.model.WtldRtuExample;
import com.douyu.water.core.model.WtldSzyData;
import com.douyu.water.core.model.WtldSzyRtu;
import com.douyu.water.core.model.WtldSzyRtuExample;
import com.douyu.water.core.model.ZKData;
import com.douyu.water.core.model.ZKRtu;
import com.douyu.water.core.model.ZKRtuExample;
import com.douyu.water.core.wtlddata.WtldMapper;
import com.douyu.water.core.wtldszydata.WtldSzyMapper;
import com.douyu.water.core.zkdata.ZKMapper;

@Service("equiService")
public class EquiServiceImpl implements EquiService {
	Date date=new Date();
	long nd = 1000*24*60*60;//一天的毫秒数
	@Autowired
	private RtuMapper rtuMapper;
	@Autowired
	private RTU_WMapper rtu_wMapper;
	@Autowired
	private ZKMapper zkMapper;
	@Autowired
	private WtldMapper wtldMapper;
	@Autowired
	private WtldSzyMapper wtldszyMapper;
	@Resource
	private DataService dataService;
	@Autowired 
	private AreaMapper areaMapper;
	@Override
	public List<Rtu> select(Rtu record, PagePara page,String userCode) {
		List<Rtu> list = null;
		String areaCode = selectAreaCode(userCode);
		if(!StringUtils.isBlank(areaCode))
		{
			if(record == null)
			{
				record = new Rtu();
			}
			record.setAreaCode(areaCode);
		}
		RtuExample example = createExample(record);
//		for (int i = 0; i < example.getOredCriteria().size(); i++) {
//			for (int j = 0; j < example.getOredCriteria().get(i).getCriteria().size(); j++) {
//				System.out.println(example.getOredCriteria().get(i).getCriteria().get(j).getCondition());
//				System.out.println(example.getOredCriteria().get(i).getCriteria().get(j).getValue());
//			}
//			
//		}
		int total=0;
		if(page != null)
		{
			total = rtu_wMapper.countByExample(example);
			total+=rtu_wMapper.countByExampleRtu(example);
			page.setTotal(total);
			
		}
		example.setPagePara(page);
		list = rtu_wMapper.selectByExample(example);
		List<Rtu> list2 = rtu_wMapper.selectByExampleRtu(example);

		processList(list);
		union(list,list2);
		return list;
	}
	@Override
	/**
	 * 查询rtu表
	 * 正元设备
	 * **/
	public List<Rtu> selectRtu(Rtu record, PagePara page,String userCode) {
		List<Rtu> list = null;
		String areaCode = selectAreaCode(userCode);
		if(!StringUtils.isBlank(areaCode))
		{
			if(record == null)
			{
				record = new Rtu();
			}
			record.setAreaCode(areaCode);
		}
		RtuExample example = createExample(record);
		int total =0;
		if(page != null)
		{
			total = rtu_wMapper.countByExample(example);
			page.setTotal(total);
		}
		example.setPagePara(page);
		list = rtu_wMapper.selectByExample(example);
		Data data1=new Data();
		for (Rtu rtu : list) {
			String str="";
			data1.setStationnumber(rtu.getStationnumber());
			List<Data> data=dataService.selectTop(data1, null);
			if(data!=null&&data.size()>0){
			rtu.setWaterlevel(data.get(0).getWaterlevel());
			rtu.setWatertemp(data.get(0).getWatertemperature());
			rtu.setWaterdepth(rtu.getHighlevel()-rtu.getWaterlevel());
			if((date.getTime()-data.get(0).getDatetime().getTime())/nd>=3){
				str=" 信息中断";
			}
			if(data.size()>=2){
				if((data.get(0).getDatetime().getTime()-data.get(1).getDatetime().getTime())/nd>=3){
					str=" 信息中断";
				}
			}
			if(data.get(0).getWaterlevel()<=0||data.get(0).getWatertemperature()<-20){
				str+=" 数据不正常";
			}
			Date date=data.get(0).getDatetime();
			String datetime=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
			rtu.setSamplebegintime(datetime);
			}else{
				str+=" 无数据";
			}
			if(rtu.getBattery()==null||rtu.getBattery()<3.5){	
				str+=" 电量过低";
			}
			rtu.setWarning(str);
		}
		return list;
	}
	@Override
	/**
	 * 查询tblJING表
	 * 中科水质设备
	 * 中科WaterMonitor数据库
	 * **/
	public List<ZKRtu> selectZKRtu(ZKRtu record, PagePara page, String userCode) {
		List<ZKRtu> list = null;
		String areaCode = selectAreaCode(userCode);
		if(!StringUtils.isBlank(areaCode))
		{
			if(record == null)
			{
				record = new ZKRtu();
			}
			record.setAreacode(areaCode);
		}
		ZKRtuExample example = createZKExample(record);
		int total =0;
		if(page != null)
		{
			total = zkMapper.countByExample(example);
			page.setTotal(total);
		}
		example.setPagePara(page);
		list = zkMapper.selectZkRtu(example);
		ZKData zkdata=new ZKData();
		for (ZKRtu zk : list) {
			String str="";
			zkdata.setTongyicode(zk.getTongyicode());
			List<ZKData> data=dataService.selectZKTop(zkdata, page);
			if(data!=null&&data.size()>0){
			zk.setShuiweibiaogao(data.get(0).getShuiweibiaogao());
			zk.setDimianbiaogao(data.get(0).getDimianbiaogao());
			zk.setDiandaolv(data.get(0).getDiandaolv());
			zk.setWendu(data.get(0).getWendu());
			zk.setCaijishijian(data.get(0).getCaijishijian());
			if((date.getTime()-data.get(0).getCaijishijian().getTime())/nd>=3){
				str=" 信息中断";
			}
			if(data.size()>=2){
				if((data.get(0).getCaijishijian().getTime()-data.get(1).getCaijishijian().getTime())/nd>=3){
					str=" 信息中断";
				}

			}
			}else{
				str+=" 无数据";			
			}
			zk.setWarning(str);
		}
		return list;
	}
	@Override
	/**
	 * 查询t_第四级信息表
	 * 大宇水质设备
	 * 大宇旧版水位数据库
	 * **/
	public List<WtldRtu> selectWtldRtu(WtldRtu record, PagePara page, String userCode) {
		List<WtldRtu> list = null;
		String areaCode = selectAreaCode(userCode);
		if(!StringUtils.isBlank(areaCode))
		{
			if(record == null)
			{
				record = new WtldRtu();
			}
			record.setAreacode(areaCode);
		}
		WtldRtuExample example = createWtldExample(record);
		int total =0;
		if(page != null)
		{
			total = wtldMapper.countByExample(example);
			page.setTotal(total);
		}
		example.setPagePara(page);
		list = wtldMapper.selectWtldRtu(example);
		WtldData data1=new WtldData();
		for (WtldRtu rtu : list) {
			String str="";
			int id=rtu.getId();
			List<WtldData> data=dataService.selectWtldTop(data1, id);
			if(data!=null&&data.size()>0){
			rtu.setShuiwei(data.get(0).getShuiwei());
			rtu.setDiandaolv(data.get(0).getDiandaolv());
			rtu.setShuiwen(data.get(0).getShuiwen());
			rtu.setCaijishijian(data.get(0).getCaijishijian());
			if((date.getTime()-data.get(0).getCaijishijian().getTime())/nd>=3){
				str=" 信息中断";
			}
			if(data.size()>=2){
				if((data.get(0).getCaijishijian().getTime()-data.get(1).getCaijishijian().getTime())/nd>=3){
					str=" 信息中断";
				}

			}
			}else{
				str+=" 无数据";			
			}
			rtu.setWarning(str);
		}
		return list;
	}
	@Override
	/**
	 * 查询设备信息表
	 * 大宇七参数水质设备
	 * 大宇wtldszy数据库
	 * **/
	public List<WtldSzyRtu> selectWtldSzyRtu(WtldSzyRtu record, PagePara page, String userCode) {
		List<WtldSzyRtu> list = null;
		String areaCode = selectAreaCode(userCode);
		if(!StringUtils.isBlank(areaCode))
		{
			if(record == null)
			{
				record = new WtldSzyRtu();
			}
			record.setAreacode(areaCode);
		}
		System.out.println(record.getId());
		WtldSzyRtuExample example = createWtldSzyExample(record);
		int total =0;
		if(page != null)
		{
			total = wtldszyMapper.countByExample(example);
			page.setTotal(total);
		}
		example.setPagePara(page);
		list = wtldszyMapper.selectWtldSzyRtu(example);
		WtldSzyData data1=new WtldSzyData();
		for (WtldSzyRtu rtu : list) {
			String str="";
			data1.setShebeiid(rtu.getId());
			List<WtldSzyData> data=dataService.selectWtldSzyTop(data1);
			if(data!=null&&data.size()>0){
			rtu.setWendu(data.get(0).getWendu());
			rtu.setPh(data.get(0).getPh());
			rtu.setDiandaolv(data.get(0).getDiandaolv());
			rtu.setKuanghuawu(data.get(0).getKuanghuawu());
			rtu.setLvhuawu(data.get(0).getLvhuawu());
			rtu.setShendu(data.get(0).getShendu());
			rtu.setYandu(data.get(0).getYandu());
			rtu.setCaijishijian(data.get(0).getCaijishijian());
			if((date.getTime()-data.get(0).getCaijishijian().getTime())/nd>=3){
				str=" 信息中断";
			}
			if(data.size()>=2){
				if((data.get(0).getCaijishijian().getTime()-data.get(1).getCaijishijian().getTime())/nd>=3){
					str=" 信息中断";
				}

			}
			}else{
				str+=" 无数据";			
			}
			rtu.setWarning(str);
		}
		return list;
	}
	
	@Override
	/**
	 * 检索最大水位、水温、气压差
	 * 用于我的主页左侧超链接
	 * **/
	public String selectResult()
	{
		StringBuffer result = new StringBuffer();
		Result rst = rtuMapper.selectMax();
		Map<String,Object> param = new HashMap<String,Object>();
		Result temp = null;
		//检索最大水位差
		param.put("field", "waterLevel");
		param.put("value", rst.getWaterlevel());
		temp =rtuMapper.selectMaxResult(param);
		if(temp != null)
		{
			String name = temp.getStationname();
			if(name != null)
			{
				result.append("<ul class='equil'><li><b>昨日最大水位变化:</b></li><li><a href=/water/data/selectData.do?stationnumber="+temp.getStationnumber()+"&flag=1 "+ 
	    				  "data-toggle='navtab' data-width='500' data-height='500'"+ 
	    				  "data-id='user_edit_nav' data-title='查看采集数据' data-mask='true'>"+ name +"," + Math.abs(temp.getWaterlevel()/1000)+"m</a></li><br/><br/>");
			}
			else
			{
				result.append("<li><b>昨日最大水位变化:</b></li><li><a href=/water/data/selectData.do?stationnumber="+temp.getStationnumber()+"&flag=1 "+ 
	    				  "data-toggle='navtab' data-width='500' data-height='500'"+ 
	    				  "data-id='user_edit_nav' data-title='查看采集数据' data-mask='true'>"+ Math.abs(temp.getWaterlevel()/1000)+"m</a></li><br/><br/>");
			}
		}
		//检索最大水温差
		param.put("field", "waterTemperature");
		param.put("value", rst.getWatertemperature());
		temp =rtuMapper.selectMaxResult(param);
		if(temp != null)
		{
			String name = temp.getStationname();
			String vale =String.valueOf((float)Math.abs(temp.getWatertemperature() / 100));
			if(name != null)
			{
				result.append("<li><b>昨日最大水温变化:</b></li><li><a href=/water/data/selectData.do?stationnumber="+temp.getStationnumber()+"&flag=1 "+ 
    				  "data-toggle='navtab' data-width='500' data-height='500'"+ 
    				  "data-id='user_edit_nav' data-title='查看采集数据' data-mask='true'>"+ name +"," +vale+"°C<br/></a></li><br/><br/>");
			}
			else
			{
				result.append("<li><b>昨日最大水温变化:</b></li><li><a href=/water/data/selectData.do?stationnumber="+temp.getStationnumber()+"&flag=1 "+ 
	    				  "data-toggle='navtab' data-width='500' data-height='500'"+ 
	    				  "data-id='user_edit_nav' data-title='查看采集数据' data-mask='true'>"+"昨日最大水温变化:" + vale+"°C</a></li><br/><br/>");
			}
		}
		//检索最大水温差
		param.put("field", "AtmosphericPressure");
		param.put("value", rst.getAtmosphericpressure());
		temp =rtuMapper.selectMaxResult(param);
		if(temp != null)
		{
			String name = temp.getStationname();
			if(name != null)
			{
				result.append("<li style='height:30px;'><b>昨日最大周围气压变化:</b></li><li><a href=/water/data/selectData.do?stationnumber="+temp.getStationnumber()+"&flag=1 "+ 
	    				  "data-toggle='navtab' data-width='500' data-height='500'"+ 
	    				  "data-id='user_edit_nav' data-title='查看采集数据' data-mask='true'>" + name +"," +Math.abs(temp.getAtmosphericpressure())+"pa</a></li><br/><br/>");
			}
			else
			{
				result.append("<li><b>昨日最大周围气压变化:</b></li><li><a href=/water/data/selectData.do?stationnumber="+temp.getStationnumber()+"&flag=1 "+ 
	    				  "data-toggle='navtab' data-width='500' data-height='500'"+ 
	    				  "data-id='user_edit_nav' data-title='查看采集数据' data-mask='true'>" + Math.abs(temp.getAtmosphericpressure())+"pa</a></li><br/><br/></ul>");
			}
		}
		return result.toString();
	}
	@Override
	/**
	 * 查询tf-common-rtu表
	 * 中科光大设备
	 * **/
	public List<Rtu> selectCommon(Rtu record, PagePara page,String userCode) {
		List<Rtu> list = null;
		String areaCode = selectAreaCode(userCode);
		if(!StringUtils.isBlank(areaCode))
		{
			if(record == null)
			{
				record = new Rtu();
			}
			record.setAreaCode(areaCode);
		}
		RtuExample example = createExample(record);
		if(page != null)
		{
			int total = rtu_wMapper.countByExampleRtu(example);
			page.setTotal(total);
		}
		example.setPagePara(page);
		list = rtu_wMapper.selectByExampleRtu(example);
		Data data1=new Data();
		for (Rtu rtu : list) {
			String str="";
			data1.setStationnumber(rtu.getStationnumber());
			List<Data> data=dataService.selectTop(data1, null);
			if(data!=null&&data.size()>0){
			rtu.setWaterlevel(data.get(0).getWaterlevel());
			rtu.setWatertemp(data.get(0).getWatertemperature());
			if(rtu.getHighlevel()!=0){
				rtu.setWaterdepth(rtu.getHighlevel()-rtu.getWaterlevel());
			}else{
				rtu.setWaterdepth(0-rtu.getWaterlevel());
			}
			
			if((date.getTime()-data.get(0).getDatetime().getTime())/nd>=3){
				str=" 信息中断";
			}
			if(data.get(0).getWaterlevel()<=0||data.get(0).getWatertemperature()<-20){
				str+=" 数据不正常";
			}
			if(data.size()>=2){
				if((data.get(0).getDatetime().getTime()-data.get(1).getDatetime().getTime())/nd>=3){
					str=" 信息中断";
				}
			}
			Date date=data.get(0).getDatetime();
			String datetime=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
			rtu.setSamplebegintime(datetime);
			}else{
				str+=" 无数据";
			}
			rtu.setWarning(str);
		}
		return list;
	}
	@Override
	/**
	 * 查询tf-common-rtu表
	 * 博兴大宇设备
	 * **/
	public List<Rtu> selectBoXing(Rtu record, PagePara page,String userCode) {
		List<Rtu> list = null;
		String areaCode = selectAreaCode(userCode);
		if(!StringUtils.isBlank(areaCode))
		{
			if(record == null)
			{
				record = new Rtu();
			}
			record.setAreaCode(areaCode);
		}
		RtuExample example = createExample(record);
		if(page != null)
		{
			int total = rtu_wMapper.countByExampleRtu(example);
			page.setTotal(total);
		}
		example.setPagePara(page);
		list = rtu_wMapper.selectByExampleRtu(example);
		Data data1=new Data();
		for (Rtu rtu : list) {
			String str="";
			data1.setStationnumber(rtu.getStationnumber());
			List<Data> data=dataService.selectTop(data1, null);
			if(data!=null&&data.size()>0){
			rtu.setWaterlevel(data.get(0).getWaterlevel());
			rtu.setWatertemp(data.get(0).getWatertemperature());
			if(rtu.getHighlevel()!=0){
			rtu.setWaterdepth(rtu.getHighlevel()-rtu.getWaterlevel());
			}else{
				rtu.setWaterdepth(0-rtu.getWaterlevel());
			}
			if((date.getTime()-data.get(0).getDatetime().getTime())/nd>=3){
				str=" 信息中断";
			}
			if(data.size()>=2){
				if((data.get(0).getDatetime().getTime()-data.get(1).getDatetime().getTime())/nd>=3){
					str=" 信息中断";
				}
			}
			if(data.get(0).getWaterlevel()<=0||data.get(0).getWatertemperature()<-20){
				str+=" 数据不正常";
			}
			Date date=data.get(0).getDatetime();
			String datetime=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
			rtu.setSamplebegintime(datetime);
			}else{
				str+=" 无数据";
			}
			rtu.setWarning(str);
		}
		return list;
	}
	
	/**
	 * 将两个集合合并
	 * **/
	private void union(List<Rtu> list,List<Rtu> list2)
	{
		if((list == null || list.size() == 0 ) && (list2 == null || list2.size() == 0))
		{
			return;
		}
		if(list == null && list2 != null)
		{
			list = list2;
			return;
		}
		if(list2!=null){
		for(Rtu rtu : list2)
		{
			list.add(rtu);
		}
		}
	}
	@Override
	/**
	 * 根据经纬度或测点名称查询
	 * **/
	public Rtu selectByLocation(Rtu record)
	{
		Rtu rtu  = null;
		String lng = record.getLongitude();
		String lat = record.getLatitude();
		if(StringUtils.isBlank(lng) || StringUtils.isBlank(lat))
		{
			return rtu;
		}
		RtuExample example = new RtuExample();
		RtuExample.Criteria criteria = example.createCriteria();
		if(!StringUtils.isBlank(lng))
		{
			criteria.andLongitudeLike(lng);
		}
		if(!StringUtils.isBlank(lat))
		{
			criteria.andLatitudeLike(lat);
		}
		List<Rtu> list = rtu_wMapper.selectByExample(example);
		if(list != null && list.size() > 0)
		{
			rtu = list.get(0);
			rtu.setFlag("0");//检索RTU表
		}
		else
		{//检索tf_common_rtu表			 
			List<Rtu> list_t = rtu_wMapper.selectByRecordRtu(record);
			if(list_t != null && list_t.size() > 0)
			{
				rtu = list_t.get(0);
				rtu.setFlag("1");//检索tf_common_rtu表
			}
		}
		if(rtu != null)
		{
			Double bat = rtu.getBattery();
			if(bat == null)
			{
				rtu.setBatteryPic("0");
			}
			else
			{
				int bati = bat.intValue();
				rtu.setBatteryPic(String.valueOf(bati));
			}	
		}
		return rtu;
	}
	
	@Override
	/**
	 * 根据areaCode查询地区
	 * **/
	public Area selectSingArea(String areaCode)
	{
		Area area = null;
		if(!StringUtils.isBlank(areaCode))
	    {
			area = areaMapper.selectByPrimaryKey(areaCode);
	    }
		return area;
	}
	@Override
	/**
	 * 根据rtu的测点编号查询rtu
	 * **/
	public Rtu selectByStation(Rtu record)
	{
		if(record == null || StringUtils.isBlank(record.getStationnumber()))
		{
			return null;
		}
		Rtu rtu = rtu_wMapper.selectByPrimaryKey(record.getStationnumber());	
		if(rtu == null)
		{
			
			List<Rtu> rtus=rtu_wMapper.selectByPrimaryKeyRtu(record);
			if(rtus!=null&&rtus.size()>0){
				rtu = rtus.get(0);
			}						
		}
		Double bat = rtu.getBattery();
		if(bat == null)
		{
			rtu.setBatteryPic("0");
		}
		else
		{
			int bati = bat.intValue();
			rtu.setBatteryPic(String.valueOf(bati));
		}
		Data data1=new Data();
		String str="";
			data1.setStationnumber(rtu.getStationnumber());
			List<Data> data=dataService.selectTop(data1, null);
			if(data!=null&&data.size()>0){
			rtu.setWaterlevel(data.get(0).getWaterlevel());
			rtu.setWatertemp(data.get(0).getWatertemperature());
			if((date.getTime()-data.get(0).getDatetime().getTime())/nd>=3){
				str=" 信息中断";
			}
			}else if(data!=null&&data.size()>=2){
				if((data.get(0).getDatetime().getTime()-data.get(1).getDatetime().getTime())/nd>=3){
					str=" 信息中断";
				}
			}
			if(rtu.getBattery()==null||rtu.getBattery()<3.5){	
				str+="电量过低";
			}
			rtu.setWarning(str);
		return rtu;
	}
	/**
	 * 获取格式化后的经纬度
	 * @param str
	 * @return
	 */
	private String getFormat(String str)
	{
		if(StringUtils.isBlank(str))
		{
			return null;
		}
		if(str.indexOf(".") == -1)
		{
			return str;
		}
		String zheng = str.substring(0,str.indexOf("."));
		String dotnum = str.substring(str.indexOf("."));
		dotnum = "0" + dotnum;
		try
		{
			float dot = Float.parseFloat(dotnum);
			int fen = (int)(dot * 60);
			float miaot = (dot * 60f) - fen;
			int miao = (int)(miaot * 60f + 0.5);
			zheng +=":" + fen +":" +miao;
			return zheng;
		}
		catch(Exception e){}
		return null;
	}
	/**
	 * 获取用户管理的地区
	 * @param userCode String
	 * @return String
	 */
	private String selectAreaCode(String userCode)
	{
		StringBuffer temp = new StringBuffer();
		if(StringUtils.isBlank(userCode) || userCode.trim().equals("1"))
		{
			return null;
		}
		List<UserArea> list = null;
		list = areaMapper.selectUserArea(userCode);
		if(list != null && list.size() > 0)
		{
			int size = list.size();
			for(int i = 0;i < size;i++)
			{
				if(i == (size -1 ))
				{
					temp.append(list.get(i).getAreaCode().trim());
				}
				else
				{
					temp.append(list.get(i).getAreaCode().trim()+",");
				}
			}
		}
		String areaCode = temp.toString();
		while(areaCode.trim().endsWith(","))
		{
			areaCode =areaCode.trim().substring(0,areaCode.trim().length() - 1);
		}
		return temp.toString();
	}
	/**
	 * 处理检索结果的经纬度信息,将a:b:c形式的经纬度改为a.fdslfjsdl形式的经纬度
	 * @param list List<Rtu>
	 */
	private void processList(List<Rtu> list)
	{
		if(list == null && list.size() == 0)
		{
			return;
		}
		for(Rtu rtu : list)
		{
			String lng = rtu.getLongitude();
			if(!StringUtils.isBlank(lng))
			{
				rtu.setLongitude(getLng(lng));
			}
			String lat = rtu.getLatitude();
			if(!StringUtils.isBlank(lat))
			{
				rtu.setLatitude(getLng(lat));
			}
		}
	}
	/**
	 * 获取经纬度值
	 * @param lng
	 * @return
	 */
	private String getLng(String lng)
	{
		String[] lngs = lng.split("[: ]+");
		float result = 0f;
		if(lngs.length > 0 && !StringUtils.isBlank(lngs[0]))
		{
			try
			{
				result += Float.parseFloat(lngs[0].trim());
			}
			catch(Exception e){}
		}
		if(lngs.length > 1 && !StringUtils.isBlank(lngs[1]))
		{
			try
			{
				result += (Float.parseFloat(lngs[1].trim())/60);
			}
			catch(Exception e){}
		}
		if(lngs.length > 2 && !StringUtils.isBlank(lngs[2]))
		{
			try
			{
				String temp = lngs[2].trim();
				temp = temp.substring(0,temp.length() - 1);
				temp = temp.substring(0,temp.indexOf("."));
				result += (Float.parseFloat(temp)/(60 * 60));
			}
			catch(Exception e){}
		}
		return String.valueOf(result);
	}
	
	@Override
	public List<Area> selectArea(String userCode)
	{
		List<Area> list = null;
		List<String> areaCode = null;
		if(!StringUtils.isBlank(userCode) && userCode.trim().equals("1"))
		{
			areaCode = processCode(selectAreaCode(userCode.trim()));
		}
		AreaExample example = new AreaExample();
		AreaExample.Criteria criteria = example.createCriteria();
		if(areaCode != null && areaCode.size() > 0)
		{
			criteria.andUcodeIn(areaCode);
		}
		criteria.andUcodeNotEqualTo("370000");
		list = areaMapper.selectByExample(example);
		return list;
	}
	
	private List<String> processCode(String userCode)
	{
		List<String> list = null;
		if(StringUtils.isBlank(userCode))
		{
			return list;
		}
		list = new ArrayList<String>();
		String[] code = userCode.split("[, ]+");
		for(String area : code)
		{
			if(!StringUtils.isBlank(area))
			{
				list.add(area.trim());
			}
		}
		return list;
	}
	/**
	 * 形成Rtu检索条件
	 * @param record
	 * @return
	 */
	private RtuExample createExample(Rtu record)
	{
		RtuExample example = new RtuExample();
		List<RtuExample.Criteria> list = example.getOredCriteria();
		RtuExample.Criteria criteria = null;
		
		if(record != null)
		{
			if(!StringUtils.isBlank(record.getAreaCode()))
			{
				String area = record.getAreaCode();
				String[] areas = area.split("[, ]+");
				for(String areaCode : areas)
				{
					criteria = example.createCriteria();
					criteria.andStationnumberLike("%37%");
					if(!StringUtils.isBlank(areaCode)&&areaCode.length() >4)
					{
						areaCode = areaCode.substring(0,4);
						criteria.andStationnumberLike(areaCode + "%");
					}
					if(!StringUtils.isBlank(record.getStationname()))
					{
						criteria.andStationnameLike("%" + record.getStationname().trim() + "%");
					}
					if(!StringUtils.isBlank(record.getRtuserialnumber()))
					{
						criteria.andRtuserialnumberLike("%" + record.getRtuserialnumber().trim() + "%");
					}
					if(!StringUtils.isBlank(record.getSensorserialnumber()))
					{
						criteria.andSensorserialnumberLike("%" + record.getSensorserialnumber().trim() + "%");
					}
					if(record.getSetuptime() != null)
					{
						criteria.andSetuptimeGreaterThanOrEqualTo(record.getSetuptime());
					}
					if(record.getFactory()!=null){
						criteria.andFactoryEqualTo(record.getFactory());
					}
					list.add(criteria);
				}
				
			}
			else
			{
				criteria = example.createCriteria();	
				criteria.andStationnumberLike("%37%");
				if(!StringUtils.isBlank(record.getStationnumber()))
				{
					criteria.andStationnameLike("%" + record.getStationnumber().trim() + "%");
				}
				if(!StringUtils.isBlank(record.getStationname()))
				{
					criteria.andStationnameLike("%" + record.getStationname().trim() + "%");
				}
				if(!StringUtils.isBlank(record.getRtuserialnumber()))
				{
					criteria.andRtuserialnumberLike("%" + record.getRtuserialnumber().trim() + "%");
				}
				if(!StringUtils.isBlank(record.getSensorserialnumber()))
				{
					criteria.andSensorserialnumberLike("%" + record.getSensorserialnumber().trim() + "%");
				}
				if(record.getSetuptime() != null)
				{
					criteria.andSetuptimeGreaterThanOrEqualTo(record.getSetuptime());
				}
				if(record.getFactory()!=null){
					criteria.andFactoryEqualTo(record.getFactory());
				}
			}
			
		}
		else
		{
			criteria = example.createCriteria();
			criteria.andStationnumberLike("%37%");
		}
		return example;
	}
	
	/**
	 * 形成ZKRtu检索条件
	 * @param record
	 * @return
	 */
	private ZKRtuExample createZKExample(ZKRtu record)
	{
		ZKRtuExample example = new ZKRtuExample();
		List<ZKRtuExample.Criteria> list = example.getOredCriteria();
		ZKRtuExample.Criteria criteria = null;
		
		if(record != null)
		{
			if(!StringUtils.isBlank(record.getAreacode()))
			{
				String area = record.getAreacode();
				String[] areas = area.split("[, ]+");
				for(String areaCode : areas)
				{
					criteria = example.createCriteria();
					criteria.andTongyicodeLike("%37%");
					if(!StringUtils.isBlank(areaCode)&&areaCode.length() >4)
					{
						areaCode = areaCode.substring(0,4);
						criteria.andTongyicodeLike(areaCode + "%");
					}
					if(!StringUtils.isBlank(record.getQuyu())){
						criteria.andQuyuLike("%" + record.getQuyu().trim() + "%");
					}
					list.add(criteria);
				}
			}else{
				criteria = example.createCriteria();
				criteria.andTongyicodeLike("%37%");
				if(!StringUtils.isBlank(record.getTongyicode())){
					criteria.andTongyicodeLike("%" + record.getTongyicode().trim() + "%");
				}
				if(!StringUtils.isBlank(record.getQuyu())){
					criteria.andQuyuLike("%" + record.getQuyu().trim() + "%");
				}
				
			}
			
		}	
		else
		{
			criteria = example.createCriteria();
			criteria.andTongyicodeLike("%37%");
		}
		return example;
	}
	
	
	/**
	 * 形成t_第四级信息检索条件
	 * 大宇旧版水位水质数据库
	 * @param record
	 * @return
	 */
	private WtldRtuExample createWtldExample(WtldRtu record)
	{
		WtldRtuExample example = new WtldRtuExample();
		List<WtldRtuExample.Criteria> list = example.getOredCriteria();
		WtldRtuExample.Criteria criteria = null;
		
		if(record != null)
		{
			if(!StringUtils.isBlank(record.getAreacode()))
			{
				String area = record.getAreacode();
				String[] areas = area.split("[, ]+");
				for(String areaCode : areas)
				{
					criteria = example.createCriteria();
					//criteria.andShebeicodeLike("%37%");
					if(!StringUtils.isBlank(areaCode)&&areaCode.length() >4)
					{
						areaCode = areaCode.substring(0,4);
						criteria.andShebeicodeLike(areaCode + "%");
					}
					if(!StringUtils.isBlank(record.getHaopai())){
						criteria.andHaopaiLike("%" + record.getHaopai().trim() + "%");
					}
					if(record.getId()!=0){
						criteria.andIdLike(record.getId()+"%");
					}
					list.add(criteria);
				}
			}else{
				criteria = example.createCriteria();
				//criteria.andShebeicodeLike("%37%");
				if(record.getId()!=0){
					criteria.andIdLike(record.getId()+"%");
				}
				if(!StringUtils.isBlank(record.getShebeicode())){
					criteria.andShebeicodeLike("%" + record.getShebeicode().trim() + "%");
				}
				if(!StringUtils.isBlank(record.getHaopai())){
					criteria.andHaopaiLike("%" + record.getHaopai().trim() + "%");;
				}
				
			}
			
		}	
		else
		{
			criteria = example.createCriteria();
			//criteria.andShebeicodeLike("%37%");
		}
		return example;
	}
	
	/**
	 * 形成设备信息检索条件
	 * 大宇七参数水质数据库
	 * @param record
	 * @return
	 */
	private WtldSzyRtuExample createWtldSzyExample(WtldSzyRtu record)
	{
		WtldSzyRtuExample example = new WtldSzyRtuExample();
		WtldSzyRtuExample.Criteria criteria = null;
		
		if(record != null)
		{
			criteria = example.createCriteria();
					if(record.getId()!=0)
					{
						criteria.andIdLike(record.getId()+"%");
					}
					if(!StringUtils.isBlank(record.getMingcheng())){
						criteria.andMingchengLike("%" + record.getMingcheng().trim() + "%");
					}
				}
		else
		{
			criteria = example.createCriteria();
			criteria.andMingchengLike("%");
		}
		return example;
	}
	
	/**
	 * 修改datacmd表
	 * 修改监测频率
	 * **/
	public int updateCmd(Rtu record){
		int res=rtuMapper.updatecmd(record);
		return res;
	}
	@Override
	public Rtu selectCmd(Rtu record) {
		Rtu rtu=rtuMapper.selectCmd(record.getStationnumber());
		return rtu;
	}
	@Override
	public List<Rtu> selectCmdAll() {
		List<Rtu> list=rtuMapper.selectCmdAll();
		return list;
	}
	@Override
	//修改rtu设备信息
	public int updateRtu(Rtu rtu) {
		int res=0;
		System.out.println(rtu.getFactory());
		if(rtu.getFactory()==null){
			res=rtu_wMapper.updateByPrimaryKeySelective(rtu);
		}
		else if(rtu.getFactory()==2||rtu.getFactory()==3){
			 res=rtu_wMapper.updateByPrimaryKeyCommon(rtu);
		}else{
			res=rtu_wMapper.updateByPrimaryKeySelective(rtu);
		}
		return res;
	}
	//插入正元冶达设备
	public int insert(Rtu rtu){
		int res=0;
		res=rtu_wMapper.insertSelective(rtu);
		
		return res;
	}
	public Date getdate(String date){
		Date datetime=null;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			datetime=df.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return datetime;
		
	}
	public int deleteRtu(String stationnumber){
		int res=0;
		res=rtu_wMapper.deleteByPrimaryKey(stationnumber);
		return res;
	}
	
}
	
